package com.example.administrator.myapplication.mode;

import android.app.Activity;

import com.example.administrator.myapplication.utils.DateUtils;
import com.example.administrator.myapplication.utils.ToastUtils;

import java.util.ArrayList;

import static com.example.administrator.myapplication.utils.LiteOrmUtil.getLiteOrm;


/**
 * 说明：沁的
 * 作者： 阳2012; 创建于： 2017/5/16 0:01
 */
public class InformationDataManager {
    private ArrayList<InformationListData> mDatas;
    private RefreshListener refreshListener;
    private static InformationDataManager recordDataManager;

    private InformationDataManager() {

    }

    public static InformationDataManager getInstands() {
        if (recordDataManager == null) {
            return recordDataManager = new InformationDataManager();
        } else {
            return recordDataManager;
        }
    }

    /**
     * @param title
     * @param content
     * @param url
     * @param imgPath
     * @return
     */
    public boolean save(Activity activity,String title, String content, String url, String imgPath) {
        if (content != null) {
            try {
                InformationListData data = new InformationListData();
                data.setTitle(title);
                data.setContent(content);
                data.setUrl(url);
                data.setImgPath(imgPath);
                data.setTime(DateUtils.getCurrentTimeYD());

                getLiteOrm().save(data);

                if (refreshListener != null) {
                    refreshListener.refreshCallback(true);
                    ToastUtils.getToastLong("添加成功");
                    activity.finish();
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 说明：获取存储的数据
     * 作者： 阳2012; 创建于： 2017/5/6 19:26
     */
    public static ArrayList<InformationListData> getData() {
        ArrayList<InformationListData> datas = getLiteOrm().query(InformationListData.class);
        return datas;
    }

    public void deleteData(InformationListData data) {
        getLiteOrm().delete(data);
        if (refreshListener != null) {
            refreshListener.refreshCallback(true);
        }
    }


    public interface RefreshListener {
        void refreshCallback(boolean isRefresh);
    }


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //                                                 阳2012  说明： 2017/5/6  8:50
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    public ArrayList<InformationListData> getmDatas() {
        return mDatas;
    }

    public void setRefreshListener(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }


}
